import { showToast } from "/utils/common";
import { getUrlShopId } from "/utils/commonMethod";
import request from "/utils/request"
const app = getApp()

Page({
  data: {
    textAreaValue: "",
    userInfo: null,
    systemInfo: null,

  },
  onLoad() {
    this.setData({
      userInfo: app.globalData.userInfo,
      systemInfo: app.globalData.systemInfo
    })
  },
  onTextareaInput(e) {
    this.setData({
      textAreaValue: e.detail.value
    })
  },
  viewID(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal() {
    this.setData({
      modalName: null
    })
  },
  onSubmit(e) {
    if (!e.detail.value.shopId) {
      return showToast({
        msg: "门店id不能为空"
      })
    }
    if (!e.detail.value.dpid) {
      return showToast({
        msg: "点评用户ID不能为空"
      })
    }
    const shopId = getUrlShopId(e.detail.value.shopId)
    if (!shopId) {
      return showToast({
        msg: "门店id不符合规则",
      })
    }
    if (this.data.userInfo.dpid) {
      dd.redirectTo({
        url: `/pages/privateCustomers/add-private-customer/add-private-customer?shopId=${shopId}&dpid=${this.data.userInfo.dpid}`
      })
    } else {
      //校验dpid 规则--先以30为以内 为纯数字 30为以上为数字加字母组合
      const dpIdPatter = e.detail.value.dpid.length < 30 ? /^\d{1,}$/.test(e.detail.value.dpid) : /^(\d+[a-zA-Z]+|[a-zA-Z]+\d+)([0-9a-zA-Z]*)$/.test(e.detail.value.dpid)
      if (!dpIdPatter) {
        return showToast({
          msg: '点评用户ID错误'
        })
      }
      // if (/^([0-9]|[a-z]|[A-Z]){10,}$/.test(e.detail.value.dpid) === false) {
      //   return showToast({
      //     msg: "点评用户id不符合规则"
      //   })
      // }
      dd.showLoading({
        content: "加载中..."
      })
      request({
        url: '/api/crm/user/setDpid',
        method: 'post',
        headers: {
          "Content-Type": "application/json"
        },
        data: {
          dpid: e.detail.value.dpid
        }
      }).then(res => {
        if (res.success) {
          dd.hideLoading()
          app.globalData.userInfo.dpid = res.vo.dpid
          this.setData({
            userInfo: app.globalData.userInfo
          })
          dd.redirectTo({
            url: `/pages/privateCustomers/add-private-customer/add-private-customer?shopId=${shopId}&dpid=${res.vo.dpid}`
          })
        }
      })
    }
  },
  onReset() {
    this.setData({
      textAreaValue: ""
    })
  }
});
